#ifndef __WIN_PE_FORMAT__
#define __WIN_PE_FORMAT__

namespace xm
{
#if 1
	#pragma pack(push, 4)

	//////////////////////////////////////////////////////////////////////////
	//
	//////////////////////////////////////////////////////////////////////////
	typedef char 						TI08;
	typedef short 						TI16;
	typedef long						TI32;
	typedef long long					TI64;

	typedef unsigned char 				TU08;
	typedef unsigned short 				TU16;
	typedef unsigned long				TU32;
	typedef unsigned long long			TU64;

	typedef	void						TVOD;
	typedef	char						TCHR;


	//////////////////////////////////////////////////////////////////////////
	// Dos header format
	//////////////////////////////////////////////////////////////////////////
	typedef struct _IMAGE_DOS_HEADER {      // DOS .EXE header
		TU16   e_magic;                     // Magic number
		TU16   e_cblp;                      // TU08s on last page of file
		TU16   e_cp;                        // Pages in file
		TU16   e_crlc;                      // Relocations
		TU16   e_cparhdr;                   // Size of header in paragraphs
		TU16   e_minalloc;                  // Minimum extra paragraphs needed
		TU16   e_maxalloc;                  // Maximum extra paragraphs needed
		TU16   e_ss;                        // Initial (relative) SS value
		TU16   e_sp;                        // Initial SP value
		TU16   e_csum;                      // Checksum
		TU16   e_ip;                        // Initial IP value
		TU16   e_cs;                        // Initial (relative) CS value
		TU16   e_lfarlc;                    // File address of relocation table
		TU16   e_ovno;                      // Overlay number
		TU16   e_res[4];                    // Reserved TU16s
		TU16   e_oemid;                     // OEM identifier (for e_oeminfo)
		TU16   e_oeminfo;                   // OEM information; e_oemid specific
		TU16   e_res2[10];                  // Reserved TU16s
		TI32   e_lfanew;                    // File address of new exe header
	} IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;

	typedef enum
	{
		WINPE_IMAGE_DOS_SIGNATURE                 = 0x5A4D,      // MZ
		WINPE_IMAGE_OS2_SIGNATURE                 = 0x454E,      // NE
		WINPE_IMAGE_OS2_SIGNATURE_LE              = 0x454C,      // LE
		WINPE_IMAGE_VXD_SIGNATURE                 = 0x454C,      // LE
	}IMAGE_SIGNATURE;

	//////////////////////////////////////////////////////////////////////////
	// File header format.
	//////////////////////////////////////////////////////////////////////////
	typedef struct _IMAGE_FILE_HEADER {
		TU16    Machine;
		TU16    NumberOfSections;
		TU32	TimeDateStamp;
		TU32	PointerToSymbolTable;
		TU32	NumberOfSymbols;
		TU16	SizeOfOptionalHeader;
		TU16    Characteristics;
	} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;

	typedef enum
	{
		WINPE_IMAGE_FILE_UNKNOWN				   = 0,
		WINPE_IMAGE_FILE_RELOCS_STRIPPED           = 0x0001,  // Relocation info stripped from file.
		WINPE_IMAGE_FILE_EXECUTABLE_IMAGE          = 0x0002,  // File is executable  (i.e. no unresolved externel references).
		WINPE_IMAGE_FILE_LINE_NUMS_STRIPPED        = 0x0004,  // Line numbers stripped from file.
		WINPE_IMAGE_FILE_LOCAL_SYMS_STRIPPED       = 0x0008,  // Local symbols stripped from file.
		WINPE_IMAGE_FILE_AGGRESIVE_WS_TRIM         = 0x0010,  // Aggressively trim working set
		WINPE_IMAGE_FILE_LARGE_ADDRESS_AWARE       = 0x0020,  // App can handle >2gb addresses
		WINPE_IMAGE_FILE_TU08S_REVERSED_LO         = 0x0080,  // TU08s of machine TU16 are reversed.
		WINPE_IMAGE_FILE_32BIT_MACHINE             = 0x0100,  // 32 bit TU16 machine.
		WINPE_IMAGE_FILE_DEBUG_STRIPPED            = 0x0200,  // Debugging info stripped from file in .DBG file
		WINPE_IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP   = 0x0400,  // If Image is on removable media, copy and run from the swap file.
		WINPE_IMAGE_FILE_NET_RUN_FROM_SWAP         = 0x0800,  // If Image is on Net, copy and run from the swap file.
		WINPE_IMAGE_FILE_SYSTEM                    = 0x1000,  // System File.
		WINPE_IMAGE_FILE_DLL                       = 0x2000,  // File is a DLL.
		WINPE_IMAGE_FILE_UP_SYSTEM_ONLY            = 0x4000,  // File should only be run on a UP machine
		WINPE_IMAGE_FILE_TU08S_REVERSED_HI         = 0x8000,  // TU08s of machine TU16 are reversed.
	}IMAGE_FILE_TYPE;	

	typedef enum
	{
		WINPE_IMAGE_FILE_MACHINE_UNKNOWN           = 0,
		WINPE_IMAGE_FILE_MACHINE_I386              = 0x014c,  // Intel 386.
		WINPE_IMAGE_FILE_MACHINE_R3000             = 0x0162,  // MIPS little-endian, 0x160 big-endian
		WINPE_IMAGE_FILE_MACHINE_R4000             = 0x0166,  // MIPS little-endian
		WINPE_IMAGE_FILE_MACHINE_R10000            = 0x0168,  // MIPS little-endian
		WINPE_IMAGE_FILE_MACHINE_WCEMIPSV2         = 0x0169,  // MIPS little-endian WCE v2
		WINPE_IMAGE_FILE_MACHINE_ALPHA             = 0x0184,  // Alpha_AXP
		WINPE_IMAGE_FILE_MACHINE_SH3               = 0x01a2,  // SH3 little-endian
		WINPE_IMAGE_FILE_MACHINE_SH3DSP            = 0x01a3,
		WINPE_IMAGE_FILE_MACHINE_SH3E              = 0x01a4,  // SH3E little-endian
		WINPE_IMAGE_FILE_MACHINE_SH4               = 0x01a6,  // SH4 little-endian
		WINPE_IMAGE_FILE_MACHINE_SH5               = 0x01a8,  // SH5
		WINPE_IMAGE_FILE_MACHINE_ARM               = 0x01c0,  // ARM Little-Endian
		WINPE_IMAGE_FILE_MACHINE_THUMB             = 0x01c2,
		WINPE_IMAGE_FILE_MACHINE_AM33              = 0x01d3,
		WINPE_IMAGE_FILE_MACHINE_POWERPC           = 0x01F0,  // IBM PowerPC Little-Endian
		WINPE_IMAGE_FILE_MACHINE_POWERPCFP         = 0x01f1,
		WINPE_IMAGE_FILE_MACHINE_IA64              = 0x0200,  // Intel 64
		WINPE_IMAGE_FILE_MACHINE_MIPS16            = 0x0266,  // MIPS
		WINPE_IMAGE_FILE_MACHINE_ALPHA64           = 0x0284,  // ALPHA64
		WINPE_IMAGE_FILE_MACHINE_MIPSFPU           = 0x0366,  // MIPS
		WINPE_IMAGE_FILE_MACHINE_MIPSFPU16         = 0x0466,  // MIPS
		WINPE_IMAGE_FILE_MACHINE_AXP64             = WINPE_IMAGE_FILE_MACHINE_ALPHA64,
		WINPE_IMAGE_FILE_MACHINE_TRICORE           = 0x0520,  // Infineon
		WINPE_IMAGE_FILE_MACHINE_CEF               = 0x0CEF,
		WINPE_IMAGE_FILE_MACHINE_EBC               = 0x0EBC,  // EFI TU08 Code
		WINPE_IMAGE_FILE_MACHINE_AMD64             = 0x8664,  // AMD64 (K8)
		WINPE_IMAGE_FILE_MACHINE_M32R              = 0x9041,  // M32R little-endian
		WINPE_IMAGE_FILE_MACHINE_CEE               = 0xC0EE,
	}IMAGE_MACHINE_TYPE;	

	//////////////////////////////////////////////////////////////////////////
	// Optional header format
	//////////////////////////////////////////////////////////////////////////
	typedef struct _IMAGE_DATA_DIRECTORY {
		DWORD   VirtualAddress;
		DWORD   Size;
	} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;

	enum
	{
		WINPE_IMAGE_NT_SIGNATURE					= 0x00004550,  // PE00
	};

	enum
	{
		WINPE_IMAGE_NUMBEROF_DIRECTORY_ENTRIES		= 16,
	};

	typedef struct _IMAGE_OPTIONAL_HEADER32 {
		// Standard fields.
		TU16   Magic;
		TU08   MajorLinkerVersion;
		TU08   MinorLinkerVersion;
		TU32   SizeOfCode;
		TU32   SizeOfInitializedData;
		TU32   SizeOfUninitializedData;
		TU32   AddressOfEntryPoint;
		TU32   BaseOfCode;
		TU32   BaseOfData;

		// NT additional fields.
		TU32   ImageBase;
		TU32   SectionAlignment;
		TU32   FileAlignment;
		TU16   MajorOperatingSystemVersion;
		TU16   MinorOperatingSystemVersion;
		TU16   MajorImageVersion;
		TU16   MinorImageVersion;
		TU16   MajorSubsystemVersion;
		TU16   MinorSubsystemVersion;
		TU32   Win32VersionValue;
		TU32   SizeOfImage;
		TU32   SizeOfHeaders;
		TU32   CheckSum;
		TU16   Subsystem;
		TU16   DllCharacteristics;
		TU32   SizeOfStackReserve;
		TU32   SizeOfStackCommit;
		TU32   SizeOfHeapReserve;
		TU32   SizeOfHeapCommit;
		TU32   LoaderFlags;
		TU32   NumberOfRvaAndSizes;
		IMAGE_DATA_DIRECTORY DataDirectory[WINPE_IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
	} IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32;

	typedef struct _IMAGE_OPTIONAL_HEADER64 {
		TU16    Magic;
		TU08    MajorLinkerVersion;
		TU08    MinorLinkerVersion;
		TU32   	SizeOfCode;
		TU32   	SizeOfInitializedData;
		TU32   	SizeOfUninitializedData;
		TU32   	AddressOfEntryPoint;
		TU32	BaseOfCode;
		TU64	ImageBase;
		TU32	SectionAlignment;
		TU32	FileAlignment;
		TU16    MajorOperatingSystemVersion;
		TU16    MinorOperatingSystemVersion;
		TU16    MajorImageVersion;
		TU16    MinorImageVersion;
		TU16    MajorSubsystemVersion;
		TU16    MinorSubsystemVersion;
		TU32	Win32VersionValue;
		TU32	SizeOfImage;
		TU32	SizeOfHeaders;
		TU32	CheckSum;
		TU16    Subsystem;
		TU16    DllCharacteristics;
		TU64	SizeOfStackReserve;
		TU64	SizeOfStackCommit;
		TU64	SizeOfHeapReserve;
		TU64	SizeOfHeapCommit;
		TU32	LoaderFlags;
		TU32	NumberOfRvaAndSizes;
		IMAGE_DATA_DIRECTORY DataDirectory[WINPE_IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
	} IMAGE_OPTIONAL_HEADER64, *PIMAGE_OPTIONAL_HEADER64;

	// Optional Header Magics
	typedef enum
	{
		WINPE_IMAGE_NT_OPTIONAL_HDR32_MAGIC      					= 0x10b,
		WINPE_IMAGE_NT_OPTIONAL_HDR64_MAGIC      					= 0x20b,
		WINPE_IMAGE_ROM_OPTIONAL_HDR_MAGIC       					= 0x107,
	}IMAGE_OPTIONAL_HEADER_MAGIC;

	// DllCharacteristics Entries
	typedef enum
	{
		//WINPE_IMAGE_LIBRARY_PROCESS_INIT							= 0x0001,     // Reserved.
		//WINPE_IMAGE_LIBRARY_PROCESS_TERM							= 0x0002,     // Reserved.
		//WINPE_IMAGE_LIBRARY_THREAD_INIT							= 0x0004,     // Reserved.
		//WINPE_IMAGE_LIBRARY_THREAD_TERM							= 0x0008,     // Reserved.
		WINPE_IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE					= 0x0040,     // DLL can move.
		WINPE_IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY				= 0x0080,     // Code Integrity Image
		WINPE_IMAGE_DLLCHARACTERISTICS_NX_COMPAT					= 0x0100,     // Image is NX compatible
		WINPE_IMAGE_DLLCHARACTERISTICS_NO_ISOLATION					= 0x0200,     // Image understands isolation and doesn't want it
		WINPE_IMAGE_DLLCHARACTERISTICS_NO_SEH						= 0x0400,     // Image does not use SEH.  No SE handler may reside in this image
		WINPE_IMAGE_DLLCHARACTERISTICS_NO_BIND						= 0x0800,     // Do not bind this image.
		WINPE_IMAGE_DLLCHARACTERISTICS_WDM_DRIVER					= 0x2000,     // Driver uses WDM model
		//															= 0x4000,     // Reserved.
		WINPE_IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE		= 0x8000,
	}IMAGE_DLL_CHARACTERISTICS;

	// Subsystem Values
	typedef enum
	{
		WINPE_IMAGE_SUBSYSTEM_UNKNOWN              					= 0,   	// Unknown subsystem.
		WINPE_IMAGE_SUBSYSTEM_NATIVE               					= 1,   	// Image doesn't require a subsystem.
		WINPE_IMAGE_SUBSYSTEM_WINDOWS_GUI          					= 2,   	// Image runs in the Windows GUI subsystem.
		WINPE_IMAGE_SUBSYSTEM_WINDOWS_CUI          					= 3,   	// Image runs in the Windows character subsystem.
		WINPE_IMAGE_SUBSYSTEM_OS2_CUI              					= 5,   	// image runs in the OS/2 character subsystem.
		WINPE_IMAGE_SUBSYSTEM_POSIX_CUI            					= 7,   	// image runs in the Posix character subsystem.
		WINPE_IMAGE_SUBSYSTEM_NATIVE_WINDOWS       					= 8,   	// image is a native Win9x driver.
		WINPE_IMAGE_SUBSYSTEM_WINDOWS_CE_GUI       					= 9,   	// Image runs in the Windows CE subsystem.
		WINPE_IMAGE_SUBSYSTEM_EFI_APPLICATION      					= 10,  	//
		WINPE_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER				= 11,  	//
		WINPE_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER					= 12,  	//
		WINPE_IMAGE_SUBSYSTEM_EFI_ROM								= 13,
		WINPE_IMAGE_SUBSYSTEM_XBOX									= 14,
	}IMAGE_SUBSYSTEM_TYPE;

	// Data Directory Index
	typedef enum
	{
		WINPE_IMAGE_DIRECTORY_ENTRY_EXPORT          				= 0,   	// Export Directory
		WINPE_IMAGE_DIRECTORY_ENTRY_IMPORT          				= 1,   	// Import Directory
		WINPE_IMAGE_DIRECTORY_ENTRY_RESOURCE        				= 2,   	// Resource Directory
		WINPE_IMAGE_DIRECTORY_ENTRY_EXCEPTION       				= 3,   	// Exception Directory
		WINPE_IMAGE_DIRECTORY_ENTRY_SECURITY        				= 4,   	// Security Directory
		WINPE_IMAGE_DIRECTORY_ENTRY_BASERELOC       				= 5,   	// Base Relocation Table
		WINPE_IMAGE_DIRECTORY_ENTRY_DEBUG           				= 6,   	// Debug Directory
		//WINPE_IMAGE_DIRECTORY_ENTRY_COPYRIGHT     				= 7,   	// (X86 usage)
		WINPE_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE    				= 7,   	// Architecture Specific Data
		WINPE_IMAGE_DIRECTORY_ENTRY_GLOBALPTR       				= 8,   	// RVA of GP
		WINPE_IMAGE_DIRECTORY_ENTRY_TLS             				= 9,   	// TLS Directory
		WINPE_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG    					= 10,   // Load Configuration Directory
		WINPE_IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT   					= 11,   // Bound Import Directory in headers
		WINPE_IMAGE_DIRECTORY_ENTRY_IAT            					= 12,   // Import Address Table
		WINPE_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT   					= 13,   // Delay Load Import Descriptors
		WINPE_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 					= 14,   // COM Runtime descriptor
	}IMAGE_DIRECTORY_ENTRY_INDEX;

	//////////////////////////////////////////////////////////////////////////
	// Nt header format
	//////////////////////////////////////////////////////////////////////////
	typedef struct _IMAGE_NT_HEADERS32 {
		TU32 Signature;
		IMAGE_FILE_HEADER FileHeader;
		IMAGE_OPTIONAL_HEADER32 OptionalHeader;
	} IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32;

	typedef struct _IMAGE_NT_HEADERS64 {
		TU32 Signature;
		IMAGE_FILE_HEADER FileHeader;
		IMAGE_OPTIONAL_HEADER64 OptionalHeader;
	} IMAGE_NT_HEADERS64, *PIMAGE_NT_HEADERS64;
	
	//////////////////////////////////////////////////////////////////////////
	// Section Header Format
	//////////////////////////////////////////////////////////////////////////
	enum{
		WINPE_IMAGE_SIZEOF_SHORT_NAME								= 8,
	};
	typedef struct _IMAGE_SECTION_HEADER {
		TU08    Name[WINPE_IMAGE_SIZEOF_SHORT_NAME];
		union {
			TU32   PhysicalAddress;
			TU32   VirtualSize;
		} Misc;
		TU32   VirtualAddress;
		TU32   SizeOfRawData;
		TU32   PointerToRawData;
		TU32   PointerToRelocations;
		TU32   PointerToLinenumbers;
		TU16   NumberOfRelocations;
		TU16   NumberOfLinenumbers;
		TU32   Characteristics;
	} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;

	// Section characteristics.
	typedef enum:TU32
	{	
		//WINPE_IMAGE_SCN_TYPE_REG					= 0x00000000,  // Reserved.
		//WINPE_IMAGE_SCN_TYPE_DSECT				= 0x00000001,  // Reserved.
		//WINPE_IMAGE_SCN_TYPE_NOLOAD				= 0x00000002,  // Reserved.
		//WINPE_IMAGE_SCN_TYPE_GROUP				= 0x00000004,  // Reserved.
		WINPE_IMAGE_SCN_TYPE_NO_PAD					= 0x00000008,  // Reserved.
		//WINPE_IMAGE_SCN_TYPE_COPY					= 0x00000010,  // Reserved.

		WINPE_IMAGE_SCN_CNT_CODE					= 0x00000020,  // Section contains code.
		WINPE_IMAGE_SCN_CNT_INITIALIZED_DATA		= 0x00000040,  // Section contains initialized data.
		WINPE_IMAGE_SCN_CNT_UNINITIALIZED_DATA		= 0x00000080,  // Section contains uninitialized data.

		WINPE_IMAGE_SCN_LNK_OTHER					= 0x00000100,  // Reserved.
		WINPE_IMAGE_SCN_LNK_INFO					= 0x00000200,  // Section contains comments or some other type of information.
		
		//WINPE_IMAGE_SCN_TYPE_OVER					= 0x00000400,  // Reserved.
		WINPE_IMAGE_SCN_LNK_REMOVE					= 0x00000800,  // Section contents will not become part of image.
		WINPE_IMAGE_SCN_LNK_COMDAT					= 0x00001000,  // Section contents comdat.
		
		//											= 0x00002000,  // Reserved.
		
		//WINPE_IMAGE_SCN_MEM_PROTECTED - Obsolete	= 0x00004000,
		WINPE_IMAGE_SCN_NO_DEFER_SPEC_EXC          	= 0x00004000,  // Reset speculative exceptions handling bits in the TLB entries for this section.
		WINPE_IMAGE_SCN_GPREL                      	= 0x00008000,  // Section content can be accessed relative to GP
		WINPE_IMAGE_SCN_MEM_FARDATA                	= 0x00008000,
		
		//WINPE_IMAGE_SCN_MEM_SYSHEAP  - Obsolete  	= 0x00010000,
		WINPE_IMAGE_SCN_MEM_PURGEABLE              	= 0x00020000,
		WINPE_IMAGE_SCN_MEM_16BIT                  	= 0x00020000,
		WINPE_IMAGE_SCN_MEM_LOCKED                 	= 0x00040000,
		WINPE_IMAGE_SCN_MEM_PRELOAD                	= 0x00080000,

		WINPE_IMAGE_SCN_ALIGN_1TU08S               	= 0x00100000,  //
		WINPE_IMAGE_SCN_ALIGN_2TU08S               	= 0x00200000,  //
		WINPE_IMAGE_SCN_ALIGN_4TU08S               	= 0x00300000,  //
		WINPE_IMAGE_SCN_ALIGN_8TU08S               	= 0x00400000,  //
		WINPE_IMAGE_SCN_ALIGN_16TU08S              	= 0x00500000,  // Default alignment if no others are specified.
		WINPE_IMAGE_SCN_ALIGN_32TU08S              	= 0x00600000,  //
		WINPE_IMAGE_SCN_ALIGN_64TU08S              	= 0x00700000,  //
		WINPE_IMAGE_SCN_ALIGN_128TU08S             	= 0x00800000,  //
		WINPE_IMAGE_SCN_ALIGN_256TU08S             	= 0x00900000,  //
		WINPE_IMAGE_SCN_ALIGN_512TU08S             	= 0x00A00000,  //
		WINPE_IMAGE_SCN_ALIGN_1024TU08S            	= 0x00B00000,  //
		WINPE_IMAGE_SCN_ALIGN_2048TU08S            	= 0x00C00000,  //
		WINPE_IMAGE_SCN_ALIGN_4096TU08S            	= 0x00D00000,  //
		WINPE_IMAGE_SCN_ALIGN_8192TU08S            	= 0x00E00000,  //
		// Unused                        			= 0x00F00000,
		WINPE_IMAGE_SCN_ALIGN_MASK                 	= 0x00F00000,

		WINPE_IMAGE_SCN_LNK_NRELOC_OVFL            	= 0x01000000,  // Section contains extended relocations.
		WINPE_IMAGE_SCN_MEM_DISCARDABLE            	= 0x02000000,  // Section can be discarded.
		WINPE_IMAGE_SCN_MEM_NOT_CACHED             	= 0x04000000,  // Section is not cachable.
		WINPE_IMAGE_SCN_MEM_NOT_PAGED              	= 0x08000000,  // Section is not pageable.
		WINPE_IMAGE_SCN_MEM_SHARED                 	= 0x10000000,  // Section is shareable.
		WINPE_IMAGE_SCN_MEM_EXECUTE                	= 0x20000000,  // Section is executable.
		WINPE_IMAGE_SCN_MEM_READ                   	= 0x40000000,  // Section is readable.
		WINPE_IMAGE_SCN_MEM_WRITE                  	= 0x80000000,  // Section is writeable.
	}IMAGE_SECTION_CHARACTERISTICS;

	//////////////////////////////////////////////////////////////////////////
	// 01. Export Directory
	//////////////////////////////////////////////////////////////////////////
	typedef struct _IMAGE_EXPORT_DIRECTORY {
		TU32   Characteristics;
		TU32   TimeDateStamp;
		TU16   MajorVersion;
		TU16   MinorVersion;
		TU32   Name;
		TU32   Base;
		TU32   NumberOfFunctions;
		TU32   NumberOfNames;
		TU32   AddressOfFunctions;     // RVA from base of image
		TU32   AddressOfNames;         // RVA from base of image
		TU32   AddressOfNameOrdinals;  // RVA from base of image
	} IMAGE_EXPORT_DIRECTORY, *PIMAGE_EXPORT_DIRECTORY;

	//////////////////////////////////////////////////////////////////////////
	// 02. Import Directory
	//////////////////////////////////////////////////////////////////////////
	typedef struct _IMAGE_IMPORT_DESCRIPTOR {
		union {
			TU32   Characteristics;            // 0 for terminating null import descriptor
			TU32   OriginalFirstThunk;         // RVA to original unbound IAT (PIMAGE_THUNK_DATA)
		};
		TU32   TimeDateStamp;                  // 0 if not bound,
		// -1 if bound, and real date\time stamp
		//     in IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT (new BIND)
		// O.W. date/time stamp of DLL bound to (Old BIND)

		TU32   ForwarderChain;                 // -1 if no forwarders
		TU32   Name;
		TU32   FirstThunk;                     // RVA to IAT (if bound this IAT has actual addresses)
	} IMAGE_IMPORT_DESCRIPTOR;
	typedef IMAGE_IMPORT_DESCRIPTOR UNALIGNED *PIMAGE_IMPORT_DESCRIPTOR;	

	typedef struct _IMAGE_THUNK_DATA64 {
		union {
			TU64 ForwarderString;  // PBYTE 
			TU64 Function;         // PDWORD
			TU64 Ordinal;
			TU64 AddressOfData;    // PIMAGE_IMPORT_BY_NAME
		} u1;
	} IMAGE_THUNK_DATA64;
	typedef IMAGE_THUNK_DATA64 * PIMAGE_THUNK_DATA64;

	typedef struct _IMAGE_THUNK_DATA32 {
		union {
			TU32 ForwarderString;      // PBYTE 
			TU32 Function;             // PDWORD
			TU32 Ordinal;
			TU32 AddressOfData;        // PIMAGE_IMPORT_BY_NAME
		} u1;
	} IMAGE_THUNK_DATA32;
	typedef IMAGE_THUNK_DATA32 * PIMAGE_THUNK_DATA32;

	#define WINPE_IMAGE_ORDINAL_FLAG64 0x8000000000000000
	#define WINPE_IMAGE_ORDINAL_FLAG32 0x80000000
	#define WINPE_IMAGE_ORDINAL64(Ordinal) ((TU16)(Ordinal & 0xffff))
	#define WINPE_IMAGE_ORDINAL32(Ordinal) ((TU16)(Ordinal & 0xffff))
	#define WINPE_IMAGE_SNAP_BY_ORDINAL64(Ordinal) ((Ordinal & WINPE_IMAGE_ORDINAL_FLAG64) != 0)
	#define WINPE_IMAGE_SNAP_BY_ORDINAL32(Ordinal) ((Ordinal & WINPE_IMAGE_ORDINAL_FLAG32) != 0)

	//////////////////////////////////////////////////////////////////////////
	// 03. Resource Directory
	//////////////////////////////////////////////////////////////////////////
	typedef struct _IMAGE_RESOURCE_DIRECTORY {
		TU32   Characteristics;
		TU32   TimeDateStamp;
		TU16   MajorVersion;
		TU16   MinorVersion;
		TU16   NumberOfNamedEntries;
		TU16   NumberOfIdEntries;
	//  IMAGE_RESOURCE_DIRECTORY_ENTRY DirectoryEntries[];
	} IMAGE_RESOURCE_DIRECTORY, *PIMAGE_RESOURCE_DIRECTORY;

	#define WINPE_IMAGE_RESOURCE_NAME_IS_STRING        0x80000000
	#define WINPE_IMAGE_RESOURCE_DATA_IS_DIRECTORY     0x80000000

	typedef struct _IMAGE_RESOURCE_DIRECTORY_ENTRY {
		union {
			struct {
				TU32 NameOffset:31;
				TU32 NameIsString:1;
			};
			TU32   Name;
			TU16    Id;
		};
		union {
			TU32   OffsetToData;
			struct {
				TU32   OffsetToDirectory:31;
				TU32   DataIsDirectory:1;
			};
		};
	} IMAGE_RESOURCE_DIRECTORY_ENTRY, *PIMAGE_RESOURCE_DIRECTORY_ENTRY;

	typedef struct _IMAGE_RESOURCE_DATA_ENTRY {
		TU32   OffsetToData;
		TU32   Size;
		TU32   CodePage;
		TU32   Reserved;
	} IMAGE_RESOURCE_DATA_ENTRY, *PIMAGE_RESOURCE_DATA_ENTRY;

	typedef struct _IMAGE_RESOURCE_DIRECTORY_STRING {
		TU16    Length;
		TU08    NameString[ 1 ];
	} IMAGE_RESOURCE_DIRECTORY_STRING, *PIMAGE_RESOURCE_DIRECTORY_STRING;

	typedef struct _IMAGE_RESOURCE_DIR_STRING_U {
		TU16    Length;
		TU16    NameString[ 1 ];
	} IMAGE_RESOURCE_DIR_STRING_U, *PIMAGE_RESOURCE_DIR_STRING_U;

	//////////////////////////////////////////////////////////////////////////
	// 04. Exception Table
	//////////////////////////////////////////////////////////////////////////

	//////////////////////////////////////////////////////////////////////////
	// 05. Certificate Data Directory
	//////////////////////////////////////////////////////////////////////////
	typedef struct _WIN_CERTIFICATE
	{
		TU32		dwLength;
		TU16        wRevision;
		TU16        wCertificateType;   
		//TU08      bCertificate[ANYSIZE_ARRAY];
	} WIN_CERTIFICATE, *LPWIN_CERTIFICATE;

	//////////////////////////////////////////////////////////////////////////
	// 06. Relocation Directory
	//////////////////////////////////////////////////////////////////////////
	typedef struct _IMAGE_BASE_RELOCATION {
		TU32		VirtualAddress;
		TU32		SizeOfBlock;
		//TU16		TypeOffset[1];
	} IMAGE_BASE_RELOCATION;
	typedef IMAGE_BASE_RELOCATION UNALIGNED * PIMAGE_BASE_RELOCATION;

	typedef enum
	{
		WINPE_IMAGE_REL_BASED_ABSOLUTE              = 0,
		WINPE_IMAGE_REL_BASED_HIGH                  = 1,
		WINPE_IMAGE_REL_BASED_LOW                   = 2,
		WINPE_IMAGE_REL_BASED_HIGHLOW               = 3,
		WINPE_IMAGE_REL_BASED_HIGHADJ               = 4,
		WINPE_IMAGE_REL_BASED_MIPS_JMPADDR          = 5,
		WINPE_IMAGE_REL_BASED_MIPS_JMPADDR16        = 9,
		WINPE_IMAGE_REL_BASED_IA64_IMM64            = 9,
		WINPE_IMAGE_REL_BASED_DIR64                 = 10,
	}IMAGE_RELOCATION_TYPE;

	//////////////////////////////////////////////////////////////////////////
	// 07. Debug Directory
	//////////////////////////////////////////////////////////////////////////
	typedef struct _IMAGE_DEBUG_DIRECTORY {
		TU32   Characteristics;
		TU32   TimeDateStamp;
		TU16   MajorVersion;
		TU16   MinorVersion;
		TU32   Type;
		TU32   SizeOfData;
		TU32   AddressOfRawData;
		TU32   PointerToRawData;
	} IMAGE_DEBUG_DIRECTORY, *PIMAGE_DEBUG_DIRECTORY;

	typedef enum
	{
		WINPE_IMAGE_DEBUG_TYPE_UNKNOWN          	= 0,
		WINPE_IMAGE_DEBUG_TYPE_COFF             	= 1,
		WINPE_IMAGE_DEBUG_TYPE_CODEVIEW         	= 2,
		WINPE_IMAGE_DEBUG_TYPE_FPO              	= 3,
		WINPE_IMAGE_DEBUG_TYPE_MISC             	= 4,
		WINPE_IMAGE_DEBUG_TYPE_EXCEPTION        	= 5,
		WINPE_IMAGE_DEBUG_TYPE_FIXUP            	= 6,
		WINPE_IMAGE_DEBUG_TYPE_OMAP_TO_SRC      	= 7,
		WINPE_IMAGE_DEBUG_TYPE_OMAP_FROM_SRC    	= 8,
		WINPE_IMAGE_DEBUG_TYPE_BORLAND          	= 9,
		WINPE_IMAGE_DEBUG_TYPE_RESERVED10       	= 10,
		WINPE_IMAGE_DEBUG_TYPE_CLSID            	= 11,
	}IMAGE_DEBUG_TYPE;	

	//////////////////////////////////////////////////////////////////////////
	// 08. Architecture (Reserved)
	//////////////////////////////////////////////////////////////////////////
	

	//////////////////////////////////////////////////////////////////////////
	// 09. Global Ptr (Reserved)
	//////////////////////////////////////////////////////////////////////////
	

	//////////////////////////////////////////////////////////////////////////
	// 10. TLS Table
	//////////////////////////////////////////////////////////////////////////
	typedef struct _IMAGE_TLS_DIRECTORY64 {
		TU64   StartAddressOfRawData;
		TU64   EndAddressOfRawData;
		TU64   AddressOfIndex;				// PDWORD
		TU64   AddressOfCallBacks;			// PIMAGE_TLS_CALLBACK *;
		TU32   SizeOfZeroFill;
		TU32   Characteristics;
	} IMAGE_TLS_DIRECTORY64;
	typedef IMAGE_TLS_DIRECTORY64 * PIMAGE_TLS_DIRECTORY64;

	typedef struct _IMAGE_TLS_DIRECTORY32 {
		TU32   StartAddressOfRawData;
		TU32   EndAddressOfRawData;
		TU32   AddressOfIndex;             // PDWORD
		TU32   AddressOfCallBacks;         // PIMAGE_TLS_CALLBACK *
		TU32   SizeOfZeroFill;
		TU32   Characteristics;
	} IMAGE_TLS_DIRECTORY32;
	typedef IMAGE_TLS_DIRECTORY32 * PIMAGE_TLS_DIRECTORY32;

	//////////////////////////////////////////////////////////////////////////
	// 11. Load Config Table
	//////////////////////////////////////////////////////////////////////////
	typedef struct {
		TU32   Size;
		TU32   TimeDateStamp;
		TU16   MajorVersion;
		TU16   MinorVersion;
		TU32   GlobalFlagsClear;
		TU32   GlobalFlagsSet;
		TU32   CriticalSectionDefaultTimeout;
		TU32   DeCommitFreeBlockThreshold;
		TU32   DeCommitTotalFreeThreshold;
		TU32   LockPrefixTable;            // VA
		TU32   MaximumAllocationSize;
		TU32   VirtualMemoryThreshold;
		TU32   ProcessHeapFlags;
		TU32   ProcessAffinityMask;
		TU16   CSDVersion;
		TU16   Reserved1;
		TU32   EditList;                   // VA
		TU32   SecurityCookie;             // VA
		TU32   SEHandlerTable;             // VA
		TU32   SEHandlerCount;
	} IMAGE_LOAD_CONFIG_DIRECTORY32, *PIMAGE_LOAD_CONFIG_DIRECTORY32;

	typedef struct {
		TU32  Size;
		TU32  TimeDateStamp;
		TU16  MajorVersion;
		TU16  MinorVersion;
		TU32  GlobalFlagsClear;
		TU32  GlobalFlagsSet;
		TU32  CriticalSectionDefaultTimeout;
		TU64  DeCommitFreeBlockThreshold;
		TU64  DeCommitTotalFreeThreshold;
		TU64  LockPrefixTable;				// VA
		TU64  MaximumAllocationSize;
		TU64  VirtualMemoryThreshold;
		TU64  ProcessAffinityMask;
		TU32  ProcessHeapFlags;
		TU16  CSDVersion;
		TU16  Reserved1;
		TU64  EditList;                		// VA
		TU64  SecurityCookie;          		// VA
		TU64  SEHandlerTable;          		// VA
		TU64  SEHandlerCount;
	} IMAGE_LOAD_CONFIG_DIRECTORY64, *PIMAGE_LOAD_CONFIG_DIRECTORY64;

	//////////////////////////////////////////////////////////////////////////
	// 12. Bound Import Table
	//////////////////////////////////////////////////////////////////////////
	typedef struct _IMAGE_BOUND_IMPORT_DESCRIPTOR {
		TU32 TimeDateStamp;
		TU16 OffsetModuleName;
		TU16 NumberOfModuleForwarderRefs;
		// Array of zero or more IMAGE_BOUND_FORWARDER_REF follows
	} IMAGE_BOUND_IMPORT_DESCRIPTOR,  *PIMAGE_BOUND_IMPORT_DESCRIPTOR;

	typedef struct _IMAGE_BOUND_FORWARDER_REF {
		TU32 TimeDateStamp;
		TU16 OffsetModuleName;
		TU16 Reserved;
	} IMAGE_BOUND_FORWARDER_REF, *PIMAGE_BOUND_FORWARDER_REF;

	//////////////////////////////////////////////////////////////////////////
	// 13. Import Address Table
	//////////////////////////////////////////////////////////////////////////

	//////////////////////////////////////////////////////////////////////////
	// 14. Delay Import Descriptor
	//////////////////////////////////////////////////////////////////////////
	typedef struct _IMAGE_DELAY_IMPORT_DESCRIPTOR{
		TU32 Attributes;
		TU32 Name;
		TU32 ModuleHandle;
		TU32 AddressTable;
		TU32 NameThunk;
		TU32 BoundAddressTable;
		TU32 UnloadAddressTable;
		TU32 TimeStamp;
	}IMAGE_DELAY_IMPORT_DESCRIPTOR, *LPIMAGE_DELAY_IMPORT_DESCRIPTOR;

	//////////////////////////////////////////////////////////////////////////
	// 15. CLR Runtime Header 
	//////////////////////////////////////////////////////////////////////////
	typedef struct IMAGE_COR20_HEADER
	{
		// Header versioning
		TU32					cb;
		TU16                    MajorRuntimeVersion;
		TU16                    MinorRuntimeVersion;

		// Symbol table and startup information
		IMAGE_DATA_DIRECTORY    MetaData;
		TU32					Flags;
		TU32					EntryPointToken;

		// Binding information
		IMAGE_DATA_DIRECTORY    Resources;
		IMAGE_DATA_DIRECTORY    StrongNameSignature;

		// Regular fixup and binding information
		IMAGE_DATA_DIRECTORY    CodeManagerTable;
		IMAGE_DATA_DIRECTORY    VTableFixups;
		IMAGE_DATA_DIRECTORY    ExportAddressTableJumps;

		// Precompiled image info (internal use only - set to zero)
		IMAGE_DATA_DIRECTORY    ManagedNativeHeader;
	} IMAGE_COR20_HEADER, *PIMAGE_COR20_HEADER;

#pragma pack(pop)

#endif//0
};//WinPe


#endif